$(function () {
    // 关注作者
    $('.follow_writer').click(function () {
        var a = $(this).children().text();
        var writer_id = $(this).attr('value');
        follower_writer(a, writer_id)
    });
    // 点赞文章
    $('.like_article').click(function () {
        var art_id = $(this).parent().attr('value');
        like_article(art_id)
    });
    // 收藏文章
    $('.col_article').click(function () {
        var art_id = $(this).parent().attr('value');
        collect_article(art_id)
    });
});

function follower_writer(a, writer_id) {
    if (a == "关注") {
        var params = {
            "writer_id": writer_id,
            "action": true
        }
    } else {
        var params = {
            "writer_id": writer_id,
            "action": false
        }
    }
    $.ajax({
        url: "/user/follow",
        type: "post",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.err == "0") {
                location.reload();
            } else {
                alert(resp.errmsg)
            }
        }
    })
}

function like_article(art_id) {
    var params = {"art_id": art_id};
    $.ajax({
        url: "/user/like",
        type: "post",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.err == "0") {
                location.reload();
            } else {
                alert(resp.errmsg)
            }
        }
    })
}

function collect_article(art_id) {
    var params = {"art_id": art_id};
    $.ajax({
        url: "/user/collect",
        type: "post",
        data: JSON.stringify(params),
        contentType: "application/json",
        success: function (resp) {
            if (resp.err == "0") {
                location.reload();
            } else {
                alert(resp.errmsg)
            }
        }
    })
}
